package com.artmofang.controller;

import com.artmofang.service.ExcelExportService;
import com.artmofang.service.Impl.ExportToExcelImpl;
import com.artmofang.vo.ErpStudentDownloadVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Api(value = "学生相关", tags = {"学生相关接口"})
@RestController
@RequestMapping("erp_student")
public class ExportToExcelController {
    @Autowired
    private ExcelExportService excelExportService;

    private Long getAgeByBirth(Integer b) {
        Long endTime = System.currentTimeMillis() / 1000;
        Long ageTime = endTime - b;
        return ageTime / 60 / 60 / 24 / 365;
    }

    @ApiOperation(value = "学生信息导出为excel", notes = "学生信息导出为excel", httpMethod = "GET")
    @GetMapping("/exportStudentInfo")
    public void exportStudentInfo(HttpServletResponse response, Integer oesid, Integer osc_id, Integer osc_start_time, Integer osc_end_time) {
        Map<String, Object> map = new HashMap<>();
        map.put("oesid", oesid);
        map.put("osc_id", osc_id);
        map.put("osc_start_time", osc_start_time);
        map.put("osc_end_time", osc_end_time);
        List<ErpStudentDownloadVo> list = excelExportService.studentList(map);
        String[] title = {"乐学向上20200901~20200931全部班级课时消耗"};
        String filename = "studentList.xls";
        String sheetName = "sheet1";
        String[][] content = new String[999][9];
//        String[][] content = new String[list.size()][10];
        try {
            content[0][0] = "序号";
            content[0][1] = "学员姓名";
            content[0][2] = "年龄";
            content[0][3] = "联系电话";
            content[0][4] = "班级姓名";
            content[0][5] = "课程名称";
            content[0][6] = "班主任";
            content[0][7] = "消耗课时";
            content[0][8] = "剩余课时";
            int hasUsedCourseTimesAll = 0;
            int surplusCourseTimesAll = 0;
            for (int i = 0; i < list.size(); i++) {
                int courseTimes = list.get(i).getOo_course_times();
                int surplusTimes = Integer.parseInt(list.get(i).getOscr_surplus_class());
                String hasUsedCourseTimes = String.valueOf(courseTimes - surplusTimes);
                content[i+1][0] = String.valueOf(i + 1);//序号
                content[i+1][1] = list.get(i).getOs_name();//学员姓名
                content[i+1][2] = String.valueOf(getAgeByBirth(list.get(i).getOs_birthday()));//学员年龄
                content[i+1][3] = list.get(i).getOs_phone();//学员电话
                content[i+1][4] = list.get(i).getOsc_name();//班级
                content[i+1][5] = list.get(i).getOc_name();//课程
                content[i+1][6] = list.get(i).getOu_name();//班主任
                content[i+1][7] = hasUsedCourseTimes;//消耗课时
                content[i+1][8] = list.get(i).getOscr_surplus_class();//剩余课时
//                content[i][9] = String.valueOf(list.get(i).getOo_course_times());//购买课时
                hasUsedCourseTimesAll += Integer.parseInt(hasUsedCourseTimes);
                surplusCourseTimesAll += Integer.parseInt(list.get(i).getOscr_surplus_class());
            }
            int UsedAllSub = list.size() + 2;
            int surplusAllSub = list.size() + 3;
            System.out.println("-**-**-*-*-*-*-");
            System.out.println(UsedAllSub);
            System.out.println(surplusAllSub);
            content[UsedAllSub][7] = "消耗总课时：";
            content[surplusAllSub][7] = "剩余总课时：";
            content[UsedAllSub][8] = String.valueOf(hasUsedCourseTimesAll);
            content[surplusAllSub][8] = String.valueOf(surplusCourseTimesAll);
        } catch (Exception e) {
            e.printStackTrace();
        }
        HSSFWorkbook wb = ExportToExcelImpl.getHSSFWorkbook(sheetName, title, content, null);
        try {
            // 响应到客户端
            this.setResponseHeader(response, filename);
            OutputStream os = response.getOutputStream();
            wb.write(os);
            os.flush();
            os.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void setResponseHeader(HttpServletResponse response, String fileName) {
        try {
            try {
                fileName = new String(fileName.getBytes(), "UTF-8");
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
            response.setContentType("application/vnd.ms-excel");
            response.setHeader("Content-disposition", "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8"));
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
}
